// ignore_for_file: non_constant_identifier_names

import 'package:book_app/feature/home/viewModel/home_view_model.dart';

import 'package:book_app/product/base/base_view.dart';
import 'package:book_app/product/constants/app_colors.dart';
import 'package:book_app/product/constants/app_strings.dart';
import 'package:book_app/product/constants/svg_constants.dart';
import 'package:book_app/product/extensions/context_extension.dart';
import 'package:book_app/product/models/book.dart';
import 'package:book_app/product/routes/app_routes.dart';
import 'package:book_app/product/widgets/container/book_info_container.dart';
import 'package:book_app/product/widgets/progress_indicator.dart';
import 'package:book_app/product/widgets/text/row_icon_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

class BookHomeView extends StatefulWidget {
  const BookHomeView({super.key});

  @override
  State<BookHomeView> createState() => _BookHomeViewState();
}

class _BookHomeViewState extends State<BookHomeView> {
  @override
  Widget build(BuildContext context) {
    return BaseView<HomeViewModel>(
      viewModel: HomeViewModel(),
      onModelReady: (model) {
        model.setContext(context);
      },
      onPageBuilder: (BuildContext context, HomeViewModel viewModel) {
        final homeViewModel = Provider.of<HomeViewModel>(context);
        final trendingBooks = homeViewModel.trendingBooks;
        final bestsellerBooks = homeViewModel.bestsellerBooks;
        return Scaffold(
          // backgroundColor: const Color.fromARGB(0, 0, 0, 0),
          extendBodyBehindAppBar:true,
          appBar: _buildAppBar(),
          body:Container(
            decoration: const BoxDecoration(
              image: DecorationImage(
                image: AssetImage('assets/images/appbackground.png'), // 替换为你的背景图片路径
                fit: BoxFit.cover,
              ),
            ),
            child: homeViewModel.isLoading
                ? CustomProgressIndicator(text: AppStrings.wait, indicatorColor: AppColors.orange)
                : SingleChildScrollView(
              child: Padding(
                padding: context.paddingLowHorizontal,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    headerInfoContainer(context),
                    SizedBox(height: context.dynamicHeight(0.05)),
                    trendingsHeaderRow(context),
                    TrendBooks(context, trendingBooks, homeViewModel),
                    SizedBox(height: context.dynamicHeight(0.05)),
                    recommendedHeaderRow(context),
                    BestsellerBooks(context, bestsellerBooks, homeViewModel),
                  ],
                ),
              ),
            ), // 在这里放置你的其他组件
          ),
        );
      },
    );
  }

  SizedBox TrendBooks(BuildContext context, List<Book> trendingBooks, HomeViewModel homeViewModel) {
    return SizedBox(
      height: context.dynamicHeight(0.45),
      child: ListView.builder(
        itemCount: trendingBooks.length,
        scrollDirection: Axis.horizontal,
        itemBuilder: (context, index) {
          final trendBook = trendingBooks[index];
          return BookInfoContainer(
            onTap: () {
              AppRoutes().navigateToDetail(context, trendBook);
            },
            img: trendBook.thumbnailUrl == ""
                ? Icon(
                    Icons.book,
                    size: 150,
                    color: AppColors.green,
                  )
                : Image.network(
                    trendBook.thumbnailUrl,
                    fit: BoxFit.fill,
                    width: context.dynamicWidth(0.4),
                    height: context.dynamicHeight(0.22),
                  ),
            bgColor: AppColors.darkWhite,
            title: trendBook.title,
            subColor: AppColors.darkGrey,
            subText: trendBook.author,
            titleColor: AppColors.white,
          );
        },
      ),
    );
  }

  SizedBox BestsellerBooks(BuildContext context, List<Book> bestsellerBooks, HomeViewModel homeViewModel) {
    return SizedBox(
      height: context.dynamicHeight(0.45),
      child: ListView.builder(
        itemCount: bestsellerBooks.length,
        scrollDirection: Axis.horizontal,
        itemBuilder: (context, index) {
          final bestsellerBook = bestsellerBooks[index];
          return BookInfoContainer(
            onTap: () {
              AppRoutes().navigateToDetail(context, bestsellerBook);
            },
            img: bestsellerBook.thumbnailUrl == ""
                ? Icon(
                    Icons.book,
                    size: 150,
                    color: AppColors.green,
                  )
                : Image.network(
                    bestsellerBook.thumbnailUrl,
                    fit: BoxFit.fill,
                    width: context.dynamicWidth(0.4),
                    height: context.dynamicHeight(0.22),
                  ),
            bgColor: AppColors.darkWhite,
            title: bestsellerBook.title,
            subColor: AppColors.darkGrey,
            subText: bestsellerBook.author,
            titleColor: AppColors.white,
          );
        },
      ),
    );
  }

  Container headerInfoContainer(BuildContext context) {
    return Container(
        width: context.dynamicWidth(1),
        height: context.dynamicHeight(0.2),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10),
          color: AppColors.darkWhite,
        ),
        child: Row(
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  IconTextRow(
                    icon: Icons.bookmark,
                    iconColor: AppColors.green,
                    text: AppStrings.findEasy,
                    textColor: AppColors.white,
                  ),
                  IconTextRow(
                    icon: Icons.bookmark,
                    iconColor: AppColors.green,
                    text: AppStrings.readBook,
                    textColor: AppColors.white,
                  ),
                  IconTextRow(
                    icon: Icons.bookmark,
                    iconColor: AppColors.green,
                    text: AppStrings.addLibrary,
                    textColor: AppColors.white,
                  ),
                ],
              ),
            ),
            const Spacer(),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: SvgPicture.asset(
                height: 100,
                SVGConstants.instance.bookLover,
              ),
            )
          ],
        ));
  }

  Row recommendedHeaderRow(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          AppStrings.bestseller,
          style: context.textTheme.headlineSmall?.copyWith(
            color: AppColors.white,
          ),
        ),
        Text(
          AppStrings.seeMore,
          style: context.textTheme.bodyMedium?.copyWith(
            color: AppColors.white,
          ),
        ),
      ],
    );
  }

  Row trendingsHeaderRow(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          AppStrings.trending,
          style: context.textTheme.headlineSmall?.copyWith(
            color: AppColors.white,
          ),
        ),
        Text(
          AppStrings.seeMore,
          style: context.textTheme.bodyMedium?.copyWith(
            color: AppColors.white,
          ),
        ),
      ],
    );
  }

  Row headerRow(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          AppStrings.homeHeader,
          style: context.textTheme.headlineSmall?.copyWith(
            color: AppColors.white,
          ),
        ),
      ],
    );
  }

  AppBar _buildAppBar() {
    return AppBar(
      automaticallyImplyLeading: false,
      backgroundColor: Colors.transparent,
      shadowColor: AppColors.transparent,
      elevation: 0,
      actions: [
        IconButton(
        icon: const Icon(Icons.add),
        tooltip: 'Open shopping cart',
        onPressed: () {
          // handle the press
          print("点击添加设备");
        },
      ),],
      centerTitle: true,
      title: Text(
        AppStrings.chargestation,
        style: context.textTheme.headlineSmall?.copyWith(
          color: AppColors.white,
          fontSize: 18,
          fontWeight: FontWeight.bold
        ),
      ),
    );
  }
}
